package com.lzh.persevere.service.impl;

import com.lzh.persevere.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class EmailService {

    public static final String registerEmailHtmlBefore =
            "<!DOCTYPE html>\n" +
                    "<html lang=\"zh-CN\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                    "    <title>您的注册验证码</title>\n" +
                    "    <style>\n" +
                    "        * {\n" +
                    "            margin: 0;\n" +
                    "            padding: 0;\n" +
                    "            box-sizing: border-box;\n" +
                    "        }\n" +
                    "        \n" +
                    "        body {\n" +
                    "            font-family: 'Inter', -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif;\n" +
                    "            background-color: #f9fafb;\n" +
                    "            color: #374151;\n" +
                    "            line-height: 1.6;\n" +
                    "            padding: 20px 0;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .container {\n" +
                    "            max-width: 600px;\n" +
                    "            margin: 0 auto;\n" +
                    "            background-color: #ffffff;\n" +
                    "            border-radius: 12px;\n" +
                    "            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);\n" +
                    "            overflow: hidden;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .header {\n" +
                    "            background: linear-gradient(135deg, #4f46e5, #6366f1);\n" +
                    "            padding: 30px 40px;\n" +
                    "            color: white;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .logo {\n" +
                    "            display: flex;\n" +
                    "            align-items: center;\n" +
                    "            gap: 12px;\n" +
                    "            margin-bottom: 15px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .logo-icon {\n" +
                    "            width: 40px;\n" +
                    "            height: 40px;\n" +
                    "            background-color: white;\n" +
                    "            border-radius: 8px;\n" +
                    "            display: flex;\n" +
                    "            align-items: center;\n" +
                    "            justify-content: center;\n" +
                    "            font-weight: 700;\n" +
                    "            font-size: 18px;\n" +
                    "            color: #4f46e5;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .logo-text {\n" +
                    "            font-size: 22px;\n" +
                    "            font-weight: 600;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .header-title {\n" +
                    "            font-size: 24px;\n" +
                    "            font-weight: 600;\n" +
                    "            margin-bottom: 8px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .header-subtitle {\n" +
                    "            opacity: 0.9;\n" +
                    "            font-weight: 300;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .content {\n" +
                    "            padding: 40px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .greeting {\n" +
                    "            font-size: 18px;\n" +
                    "            margin-bottom: 20px;\n" +
                    "            color: #1f2937;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .message {\n" +
                    "            margin-bottom: 30px;\n" +
                    "            color: #4b5563;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .message p {\n" +
                    "            margin-bottom: 16px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .code-container {\n" +
                    "            background-color: #f3f4f6;\n" +
                    "            border-radius: 8px;\n" +
                    "            padding: 30px 20px;\n" +
                    "            margin: 30px 0;\n" +
                    "            text-align: center;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .code {\n" +
                    "            font-size: 36px;\n" +
                    "            font-weight: 700;\n" +
                    "            letter-spacing: 8px;\n" +
                    "            color: #4f46e5;\n" +
                    "            margin: 0;\n" +
                    "            font-family: 'Courier New', Courier, monospace;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .code-note {\n" +
                    "            color: #6b7280;\n" +
                    "            font-size: 14px;\n" +
                    "            margin-top: 12px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .instructions {\n" +
                    "            background-color: #eff6ff;\n" +
                    "            border-left: 4px solid #3b82f6;\n" +
                    "            padding: 16px;\n" +
                    "            border-radius: 0 6px 6px 0;\n" +
                    "            margin: 25px 0;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .instructions-title {\n" +
                    "            font-weight: 600;\n" +
                    "            margin-bottom: 8px;\n" +
                    "            color: #1e40af;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .instructions-list {\n" +
                    "            padding-left: 20px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .instructions-list li {\n" +
                    "            margin-bottom: 8px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .security-note {\n" +
                    "            color: #6b7280;\n" +
                    "            font-size: 14px;\n" +
                    "            margin: 30px 0;\n" +
                    "            padding: 15px;\n" +
                    "            background-color: #fef2f2;\n" +
                    "            border-radius: 6px;\n" +
                    "            border: 1px solid #fee2e2;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .security-note strong {\n" +
                    "            color: #dc2626;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .footer {\n" +
                    "            padding: 25px 40px;\n" +
                    "            background-color: #f9fafb;\n" +
                    "            border-top: 1px solid #f3f4f6;\n" +
                    "            text-align: center;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .footer-links {\n" +
                    "            display: flex;\n" +
                    "            justify-content: center;\n" +
                    "            gap: 20px;\n" +
                    "            margin-bottom: 15px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .footer-link {\n" +
                    "            color: #4f46e5;\n" +
                    "            text-decoration: none;\n" +
                    "            font-size: 14px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .footer-link:hover {\n" +
                    "            text-decoration: underline;\n" +
                    "        }\n" +
                    "        \n" +
                    "        .copyright {\n" +
                    "            color: #9ca3af;\n" +
                    "            font-size: 13px;\n" +
                    "        }\n" +
                    "        \n" +
                    "        @media (max-width: 600px) {\n" +
                    "            .container {\n" +
                    "                border-radius: 0;\n" +
                    "                box-shadow: none;\n" +
                    "                width: 100%;\n" +
                    "            }\n" +
                    "            \n" +
                    "            .header, .content, .footer {\n" +
                    "                padding: 25px 20px;\n" +
                    "            }\n" +
                    "            \n" +
                    "            .code {\n" +
                    "                font-size: 28px;\n" +
                    "                letter-spacing: 6px;\n" +
                    "            }\n" +
                    "            \n" +
                    "            .footer-links {\n" +
                    "                flex-direction: column;\n" +
                    "                gap: 10px;\n" +
                    "            }\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "    <div class=\"container\">\n" +
                    "        <!-- 头部区域 -->\n" +
                    "        <div class=\"header\">\n" +
                    "            <div class=\"logo\">\n" +
                    "                <div class=\"logo-icon\">z</div>\n" +
                    "                <div class=\"logo-text\">zhiheng.lu</div>\n" +
                    "            </div>\n" +
                    "            <h1 class=\"header-title\">完成您的注册验证</h1>\n" +
                    "            <p class=\"header-subtitle\">我们已收到您的注册申请，请使用下方验证码完成验证</p>\n" +
                    "        </div>\n" +
                    "        \n" +
                    "        <!-- 内容区域 -->\n" +
                    "        <div class=\"content\">\n" +
                    "            <p class=\"greeting\">您好</p>\n" +
                    "            <div class=\"message\">\n" +
                    "                <p>感谢您注册 <strong>zhiheng.lu</strong> 账户！为了确保是您本人操作并保护您的账户安全，请使用以下验证码完成注册流程。</p>\n" +
                    "            </div>\n" +
                    "            \n" +
                    "            <!-- 验证码区域 -->\n" +
                    "            <div class=\"code-container\">\n" +
                    "                <p class=\"code\">";

    public static final String registerEmailHtmlAfter = "</p>\n" +
            "                <p class=\"code-note\">验证码有效期为 15 分钟，请尽快使用</p>\n" +
            "            </div>\n" +
            "            \n" +
            "            <!-- 操作指引 -->\n" +
            "            <div class=\"instructions\">\n" +
            "                <p class=\"instructions-title\">如何完成验证？</p>\n" +
            "                <ol class=\"instructions-list\">\n" +
            "                    <li>返回注册页面，在\"验证码\"输入框中填写上方6位数字</li>\n" +
            "                    <li>点击\"验证并完成注册\"按钮</li>\n" +
            "                    <li>验证成功后，您将直接进入您的账户首页</li>\n" +
            "                </ol>\n" +
            "            </div>\n" +
            "            \n" +
            "            <!-- 安全提示 -->\n" +
            "            <p class=\"security-note\">\n" +
            "                <strong>安全提示：</strong> 验证码是您账户安全的重要保障，请勿向任何第三方泄露。\n" +
            "            </p>\n" +
            "        </div>\n" +
            "        \n" +
            "        <!-- 页脚区域 -->\n" +
            "        <!-- <div class=\"footer\">\n" +
            "            <div class=\"footer-links\">\n" +
            "                <a href=\"#\" class=\"footer-link\">帮助中心</a>\n" +
            "                <a href=\"#\" class=\"footer-link\">隐私政策</a>\n" +
            "                <a href=\"#\" class=\"footer-link\">用户协议</a>\n" +
            "                <a href=\"#\" class=\"footer-link\">联系我们</a>\n" +
            "            </div>\n" +
            "            <p class=\"copyright\">© 2023 SiteName. 保留所有权利。</p>\n" +
            "        </div> -->\n" +
            "    </div>\n" +
            "</body>\n" +
            "</html>";

    @Value("${spring.mail.username}")
    private String from;

    public boolean sendRegisterEmail(String to, String subject, String code) {
        try {
            // 邮件配置
            JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            // 设置发送方邮箱地址
            helper.setFrom(String.format("zhiheng.lu<%s>", from));
            // 设置接收方邮箱地址
            helper.addTo(to);
            // 邮件内容
            helper.setText(registerEmailHtmlBefore + code + registerEmailHtmlAfter, true);
            // 邮件主题
            helper.setSubject(subject);
            // 发送邮件
            mailSender.send(message);
        } catch (MessagingException e) {
            return false;
        }
        return true;
    }
}
